package com.imooc.newretail.bpl.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.newretail.bpl.form.RoleForm;
import com.imooc.newretail.common.exceptions.BplException;
import com.imooc.newretail.common.utils.BeanUtils;
import com.imooc.newretail.common.vo.RV;
import com.imooc.newretail.common.vo.ResultVO;
import com.imooc.newretail.bpl.dto.RoleDTO;
import com.imooc.newretail.bpl.dto.TenantDTO;
import com.imooc.newretail.bpl.entity.Role;
import com.imooc.newretail.bpl.entity.RolePermission;
import com.imooc.newretail.bpl.enums.ErrCodeEnum;
import com.imooc.newretail.bpl.service.IRoleService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.imooc.newretail.common.BaseController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author Hank
 * @since 2020-06-27
 */
@Api(tags = "系统角色管理")
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController<IRoleService> {

    @Autowired
    IRoleService roleService;

    /***
     * 添加或修改角色
     * @param roleForm
     * @return
     */
    @PostMapping("/")
    public ResultVO saveOrUpdate(@RequestBody RoleForm roleForm) {
        Role r = BeanUtils.copyProperties(roleForm, Role.class);
        boolean dotype = roleService.saveOrUpdate(r);
        return dotype ? RV.success(dotype) : RV.fail("执行失败");
    }

    /***
     * 查询角色列表
     * @param cuurentPage 当前页
     * @param pageRow 每页显示行数
     * @tenantId
     * @return
     */
    @GetMapping("/list")
    public ResultVO findList(@RequestParam("tenantId") String tenantId, @RequestParam("currentPage") Integer cuurentPage, @RequestParam("pageRow") Integer pageRow) {
        List<TenantDTO> tenantDTOS = new ArrayList<>();
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotEmpty(tenantId)) {
            query.eq("tenant_id", tenantId);
        }
        query.orderByAsc("create_time");
        return RV.success(roleService.page(new Page<>(cuurentPage, pageRow), query));
    }

    @GetMapping("/{roleId}/permissons")
    public ResultVO findPermissonByRoleId(@PathVariable("roleId") String roleId, @RequestParam("tenantId") String tenantId, @RequestParam("currentPage") Integer cuurentPage, @RequestParam("pageRow") Integer pageRow) {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotEmpty(tenantId)) {
            query.eq("role_id", tenantId);
        }
        query.orderByAsc("create_time");
        return RV.success(roleService.page(new Page<>(cuurentPage, pageRow), query));
    }

    @PostMapping("/{roleId}/addpermissons")
    public ResultVO addpermissons(@PathVariable("roleId") String roleId, @RequestBody List<String> permissonIds) {
        if (permissonIds == null || permissonIds.isEmpty()) {
            throw new BplException(ErrCodeEnum.PARAM_IS_EMPTY);
        }
        List<RolePermission> permissionList = new ArrayList<>(permissonIds.size());
        for (String permissonId : permissonIds) {
            RolePermission p = new RolePermission();
            p.setRoleId(roleId).setMenuId(permissonId);
            permissionList.add(p);
        }
        boolean dotype = roleService.savePermissons(permissionList);
        return dotype ? RV.success(dotype) : RV.fail("操作失败!");
    }
}
